
var oldColor;
function getContact(order){
	arry.splice(0,arry.length);
	if(order==0){
		$("#order").replaceWith('<img onclick="getContact(1)" title="按倒序排列" style="cursor:pointer" class="cursor1" id="order" src="images/down.gif" />');
		getContactByOrder(0);
	}
	if(order==1){
		$("#order").replaceWith('<img onclick="getContact(0)" title="按顺序排列" style="cursor:pointer" class="cursor1" id="order" src="images/up.gif" />');
		getContactByOrder(1);
	}
}

var tmpId=0;
function modContact(contactId){
	tmpId=contactId;
	tb_show("修改联系人信息","addContact.html?TB_iframe=true&height=400&width=600&modal=false",null);
}

function getContactByOrder(order){
	changeTitle("contacts");
	arry=[];
	$("#contacts .STYLE7").remove();
	$.post("getContactByUserId?order="+order,function(data){
		if(data==""||data==null){
			return;
		}
		var contacts = new Array();
		contacts = getModelArray(data);
		for(var i=0;i<contacts.length;i++){
			var contact = contacts[i];
			var tr = $('<tr align="center" id='+contact.contactId+'></tr>');
			if(i%2==0){
				tr.css("background-color","#FFFFFF");					
			}else{
				tr.css("background-color","#EBF4FD");	
			}
			tr.addClass("STYLE7");
			tr.mouseover(function(){
				oldColor = $(this).css("background-color");
				$(this).css("background-color","#EEEEEE");
			});
			tr.mouseout(function(){
				$(this).css("background-color",oldColor);
			});
			tr.html('<td height="20"><input onclick="oncheck(this)" id="contact'+contact.contactId+'" type="checkbox"/></td><td>'+contact.fullName+'</td><td>'+contact.sex+'</td><td>&nbsp;'+contact.mobilePhone+'&nbsp;</td><td>&nbsp;'+contact.email+'&nbsp;</td><td>&nbsp;'+contact.QQ+'&nbsp;</td><td>&nbsp;'+contact.MSN+'&nbsp;</td><td><a href="javascript:getContactByGroupId('+contact.groupId+')">'+contact.groupName+'</a></td><td><a href="javascript:modContact('+contact.contactId+')">详细/编辑</a></td><td><img style="cursor:pointer" onclick="JavaScript:oneForwardTo('+contact.contactId+')" class="cursor1" title="转移到" src="images/forward.gif"/></td><td><img title="删除" onclick="JavaScript:delContactById('+contact.contactId+')" style="cursor:pointer" class="cursor1"  src="images/delAll.gif" /></td>').appendTo($("#contacts"));
		}
	},"text");
}

function delChecked(){
	if (arry.length>0) {
		$.post("delContactByContactId",{"list":arry.toString()},function(data){
			if(data=="true"){
				show_Message("删除联系人完成","green");
				getContact(0);
			}else{
				show_Message("删除联系人失败,请稍后重试","red");
			}
		},"text");
	} else {
		show_Message("请选择需要删除的联系人","red");
	}
}

var tmpGroupId=0;
function forwardTo(){
	if (arry.length>0) {
		tb_show("请选择您要转移到的群组","forwardTo?TB_iframe=true&height=250&width=410&modal=false",null);
		//<a href="forwardTo?TB_iframe=true&height=250&width=410"  class="thickbox" ></a>
	} else {
		show_Message("请选择您要转移的联系人","red");
	}
}
function oneForwardTo(index){
	tmpGroupId=index;
	tb_show("请选择您要转移到的群组","forwardTo?TB_iframe=true&height=250&width=410&modal=false",null);
}

var timeout;
function instant(){
	var search = trim($("#search").val());
	if(search.length>0){
		arry=[];
		search = encodeURI(search);
		clearTimeout(timeout);
		timeout=setTimeout(function(){
			$.post("getContactByKeyWord",{"keyWord":search}, function(data){
				if (data == "" || data == null) {
					$("#contacts .STYLE7").remove();
					var tr = $('<tr align="center"></tr>');
					tr.css("background-color", "#EBF4FD");
					tr.addClass("STYLE7");
					tr.mouseover(function(){
						oldColor = $(this).css("background-color");
						$(this).css("background-color", "#EEEEEE");
					});
					tr.mouseout(function(){
						$(this).css("background-color", oldColor);
					});
					tr.html("抱歉,未找到任何数据!");
					return;
				}
				arry=[];
				var contacts = new Array();
				contacts = getModelArray(data);
				$("#contacts .STYLE7").remove();
				changeTitle("contacts");
				for (var i = 0; i < contacts.length; i++) {
					var contact = contacts[i];
					var tr = $('<tr align="center" id='+contact.contactId+'></tr>');
					if (i % 2 == 0) {
						tr.css("background-color", "#FFFFFF");
					}
					else {
						tr.css("background-color", "#EBF4FD");
					}
					tr.addClass("STYLE7");
					tr.mouseover(function(){
						oldColor = $(this).css("background-color");
						$(this).css("background-color", "#EEEEEE");
					});
					tr.mouseout(function(){
						$(this).css("background-color", oldColor);
					});
					tr.html('<td height="20"><input onclick="oncheck(this)" id="contact'+contact.contactId+'" type="checkbox"/></td><td>'+contact.fullName+'</td><td>'+contact.sex+'</td><td>&nbsp;'+contact.mobilePhone+'&nbsp;</td><td>&nbsp;'+contact.email+'&nbsp;</td><td>&nbsp;'+contact.QQ+'&nbsp;</td><td>&nbsp;'+contact.MSN+'&nbsp;</td><td><a href="javascript:getContactByGroupId('+contact.groupId+')">'+contact.groupName+'</a></td><td><a href="javascript:modContact('+contact.contactId+')">详细/编辑</a></td><td><img style="cursor:pointer" onclick="JavaScript:oneForwardTo('+contact.contactId+')" class="cursor1" title="转移到" src="images/forward.gif"/></td><td><img title="删除" onclick="JavaScript:delContactById('+contact.contactId+')" style="cursor:pointer" class="cursor1"  src="images/delAll.gif" /></td>').appendTo($("#contacts"));
				}
			}, "test");
		},100);
		}else{
			clearTimeout(timeout);
			changeTitle("contacts");
			getContactByOrder(0);
		}
}

function delContactById(contactId){
	$.post("delContactByContactId",{"list":contactId}, function(data){
		if(data=="true"){
			$("tr[id="+contactId+"]").remove();
			show_Message("删除成功!","green");
		}else{
			show_Message("删除失败,请稍后再试!","red");
		}
	}, "test");
}
